package cn.linstudy.blog.service.impl;

import cn.linstudy.blog.dto.DictionaryByNameDTO;
import cn.linstudy.blog.dto.DictionaryDTO;
import cn.linstudy.blog.dto.DictionaryVagueDTO;
import cn.linstudy.blog.entity.Dictionary;
import cn.linstudy.blog.enums.ErrorCodeEnum;
import cn.linstudy.blog.mapper.DictionaryMapper;
import cn.linstudy.blog.service.DictionaryService;
import cn.linstudy.blog.utils.BeanConvertUtil;
import cn.linstudy.blog.vo.BaseVO;
import cn.linstudy.blog.vo.CommonListVO;
import cn.linstudy.blog.vo.DictionaryByIdVO;
import cn.linstudy.blog.vo.DictionaryVO;
import cn.linstudy.blog.vo.DictionaryVagueVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @Author XiaoLin
 * @Date 2021/12/22 17:11
 * @Description
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    public CommonListVO<DictionaryVO> list(DictionaryByNameDTO dictionaryByNameDTO) {
        CommonListVO<DictionaryVO> commonListVO = new CommonListVO<>();
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<Dictionary>()
            .eq(Dictionary::getName, dictionaryByNameDTO.getName());
        List<Dictionary> list = dictionaryMapper.selectList(queryWrapper);
        List<DictionaryVO> voList = BeanConvertUtil.convertInstance().convert(list, DictionaryVO.class);
        commonListVO.setVoList(voList);
        return commonListVO;
    }

    @Override
    public CommonListVO<DictionaryVagueVO> query(DictionaryVagueDTO dictionaryVagueDTO) {
        //解决初始参数解密后空数据是null问题,在做数据操作时候就会出现空指针异常
        if (StringUtils.isEmpty(dictionaryVagueDTO.getMeaning())) {
            dictionaryVagueDTO.setMeaning("");
        }
        if (StringUtils.isEmpty(dictionaryVagueDTO.getName())) {
            dictionaryVagueDTO.setName("");
        }
        CommonListVO<DictionaryVagueVO> commonListVO = new CommonListVO();
        List<Dictionary> dictionaryDOList = dictionaryMapper.selectList(null);
        dictionaryDOList = dictionaryDOList.stream().filter(s ->
            s.getName().indexOf(dictionaryVagueDTO.getName()) != -1 &&
                s.getMeaning().indexOf(dictionaryVagueDTO.getMeaning()) != -1
        ).sorted(Comparator.comparing(Dictionary::getUpdateTime).reversed()).collect(Collectors.toList());
        //list的大小
        int count = dictionaryDOList.size();
        //总数据
        commonListVO.setTotal(Long.valueOf(count));
        //每页的开始数
        dictionaryVagueDTO.setPageNo((dictionaryVagueDTO.getPageNo() - 1) * dictionaryVagueDTO.getPageSize());
        //list进行分页
        dictionaryDOList = dictionaryDOList.subList(dictionaryVagueDTO.getPageNo(), count - dictionaryVagueDTO.getPageNo() > dictionaryVagueDTO.getPageSize() ? dictionaryVagueDTO.getPageNo() + dictionaryVagueDTO.getPageSize() : count);
        //在把集合赋值给前端要展示的对象
        List<DictionaryVagueVO> dictionaryVagueList = BeanConvertUtil
            .convertInstance().convert(dictionaryDOList, DictionaryVagueVO.class);

        commonListVO.setVoList(dictionaryVagueList);
        return commonListVO;
    }

    @Override
    public BaseVO add(DictionaryDTO dictionaryDTO, String username) {
        //同一个业务码下不允许存在相同编码
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(dictionaryQueryWrapper ->
            dictionaryQueryWrapper.eq(Dictionary::getName, dictionaryDTO.getName())
                                    .eq(Dictionary::getValue, dictionaryDTO.getValue()));
        Dictionary dictionary = dictionaryMapper.selectOne(queryWrapper);
        if (null != dictionary) {
            return new BaseVO(false, ErrorCodeEnum.CODE_IS_NOT_SAME.getKey(), ErrorCodeEnum.CODE_IS_NOT_SAME.getValue());
        }
        Dictionary dictionaryDO = new Dictionary();
        BeanUtils.copyProperties(dictionaryDTO, dictionaryDO);
        dictionaryDO.setCreateTime(new Date());
        dictionaryDO.setCreateUser(username);
        dictionaryDO.setUpdateTime(new Date());
        dictionaryDO.setUpdateUser(username);
        dictionaryMapper.insert(dictionaryDO);
        return new BaseVO();
    }

    @Override
    public BaseVO update(DictionaryDTO dictionaryDTO, Integer id, String username) {
        Dictionary dictionaryDO = new Dictionary();
        BeanUtils.copyProperties(dictionaryDTO, dictionaryDO);
        dictionaryDO.setUpdateUser(username);
        dictionaryDO.setUpdateTime(new Date());
        dictionaryDO.setId(id);
        dictionaryMapper.updateById(dictionaryDO);
        return new BaseVO();
    }

    @Override
    public BaseVO delete(Integer id) {
        dictionaryMapper.deleteById(id);
        return new BaseVO();
    }

    @Override
    public DictionaryByIdVO queryDictionaryById(Integer id) {
        Dictionary dictionaryDO = dictionaryMapper.selectById(id);
        DictionaryByIdVO dictionaryByIdVO = new DictionaryByIdVO();
        BeanUtils.copyProperties(dictionaryDO, dictionaryByIdVO);
        return dictionaryByIdVO;
    }
}